package api;

import Utils.SessionUtil;
import cn.hutool.json.JSONUtil;
import common.ApplcationVariable;
import model.ResultData;
import model.UserInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/sess")
public class GetSessionServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType(ApplcationVariable.RESPONSE_CONTENT_TYPE);
        int state=-1;
        String msg="";
        String data="";
        UserInfo userInfo= SessionUtil.getSessionUserInfo(req);
        if(userInfo!=null){
            //已经登录
            state=200;
            data=userInfo.getUsername();
            msg="已经登录";
        }else {
            //未登录
            data="";
            msg="未登录";
        }
        resp.getWriter().println(JSONUtil.toJsonStr(new ResultData(state,data,msg)));
    }
}
